package dice.sinanya.dice.manager;

import com.forte.qqrobot.beans.messages.result.inner.Group;
import dice.sinanya.dice.MakeNickToSender;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.BanMasterOrManagerGroupException;
import dice.sinanya.exceptions.CantInputBanException;
import dice.sinanya.exceptions.NotEnableBanException;
import dice.sinanya.exceptions.NotFoundGroupException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static dice.sinanya.system.MessagesTag.*;
import static dice.sinanya.tools.checkdata.CheckIsNumbers.isNumeric;
import static dice.sinanya.tools.checkdata.imal.LeaveGroup.leaveGroup;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityBanProperties;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.SwitchBot.botOff;
import static dice.sinanya.tools.getinfo.SwitchBot.botOn;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;

/**
 * @author SitaNya
 * 日期: 2019-08-14
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明:
 */
public class Admin implements MakeNickToSender {
    private static final Logger log = LogManager.getLogger(Admin.class.getName());
    private final EntityTypeMessages entityTypeMessages;

    public Admin(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void on() {
        String tag = TAG_ADMIN_ON;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 4));
        if (entityBanProperties.getMaster().contains(entityTypeMessages.getFromQqString())) {
            botOn(Long.parseLong(msg));
            entityTypeMessages.getMsgSender().SENDER.sendGroupMsg(msg, entitySystemProperties.getBotStart(entityTypeMessages.getFromGroupString()));
            sender(entityTypeMessages, "已在群:\t" + msg + "\t中开启");
        } else {
            sender(entityTypeMessages, entityBanProperties.getNotMaster());
        }
    }

    public void off() throws InterruptedException {
        String tag = TAG_ADMIN_OFF;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 4));
        if (entityBanProperties.getMaster().contains(entityTypeMessages.getFromQqString())) {
            botOff(Long.parseLong(msg));
            entityTypeMessages.getMsgSender().SENDER.sendGroupMsg(msg, entitySystemProperties.getBotStop(entityTypeMessages.getFromGroupString()));
            sender(entityTypeMessages, "已在群:\t" + msg + "\t中关闭");
        } else {
            sender(entityTypeMessages, entityBanProperties.getNotMaster());
        }
    }

    public void ban() throws NotEnableBanException, CantInputBanException, NotFoundGroupException, BanMasterOrManagerGroupException {
        String tag = TAG_ADMIN_BAN;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 4));
        if (msg.contains(" ") && isNumeric(msg.split(" ")[0])) {
            entityTypeMessages.getMsgGet().setMsg(".admin exit " + msg.split(" ")[0]);
        } else {
            sender(entityTypeMessages, "输入参数有误，请按照此格式`.admin ban <号码> <理由>`其中理由必填");
            return;
        }
        exit();
        entityTypeMessages.getMsgGet().setMsg(".ban group " + msg);
        new BanList(entityTypeMessages).inputGroupBanList();
    }

    public void exit() throws NotFoundGroupException {
        String tag = TAG_ADMIN_EXIT;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 4));

        if (!isNumeric(msg)) {
            throw new NotFoundGroupException(entityTypeMessages);
        }

        if (entityBanProperties.getMaster().contains(entityTypeMessages.getFromQqString())) {
            try {
                entityTypeMessages.getMsgSender().SENDER.sendGroupMsg(msg, entitySystemProperties.getBotExit(entityTypeMessages.getFromGroupString()));
                Thread.sleep(500);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
                Thread.currentThread().interrupt();
            }
            leaveGroup(entityTypeMessages, msg);
            sender(entityTypeMessages, "已退出群:\t" + msg);
        } else {
            sender(entityTypeMessages, entityBanProperties.getNotMaster());
        }
    }

    public void search() {
        String tag = TAG_ADMIN_SEARCH;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 4));
        long qqId;
        if (!isNumeric(msg)) {
            sender(entityTypeMessages, "请输入QQ号码");
            return;
        } else {
            qqId = Long.parseLong(msg);
        }
        StringBuilder hasGroup = new StringBuilder();
        hasGroup.append("你和指定人同处于以下群中:");
        Group[] groups = entityTypeMessages.getMsgSender().GETTER.getGroupList().getList();
        for (Group group : groups) {
            if (entityTypeMessages.getMsgSender().GETTER.getGroupMemberInfo(group.getCode(), String.valueOf(qqId)) != null) {
                hasGroup.append("\n").append(makeGroupNickToSender(group.getName())).append("(").append(group.getCode()).append(")");
            }
        }
        sender(entityTypeMessages, hasGroup.toString());
    }
}
